package com.yyl.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yyl.common.pojo.PictureResult;
import com.yyl.common.utils.fastDFSClient;

@Service
public class PictureServiceImpl implements PictureService {

	@Value("${IMG_SERVER_BASE_URL}")
	private String IMG_SERVER_BASE_URL;
	@Override
	public PictureResult uploadPicture(MultipartFile picFile) throws Exception {
		
		PictureResult pictureResult = new PictureResult();;
		// 判断图片是否为空
		if (picFile.isEmpty()) {
			
			pictureResult.setError(1);
			pictureResult.setMessage("图片为空");
			return pictureResult;
		}
		//取图片扩展名(不要.)
		String originalFile = picFile.getOriginalFilename();
		String extName = originalFile.substring(originalFile.indexOf(".")+1);
		//上传到图片服务器
		fastDFSClient fastDFSClient = new fastDFSClient("/WEB-INF/properties/client.conf");
		String url = fastDFSClient.uploadFile(picFile.getBytes(), extName);
		//拼接图片服务器的ip地址
		url = IMG_SERVER_BASE_URL+url;
		//把url响应给客户端
		pictureResult.setError(0);
		pictureResult.setUrl(url);
		return pictureResult;
	}

}
